package com.shanghaionstar.discover.presenter;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.IMVPView;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.framework.utils.ExceptionUtil;
import com.onstar.cn.mag.ovd.ManagedContentDetailDTO;
import com.onstar.cn.more.entity.ContentHeaderType;
import com.onstar.cn.more.response.GetAllContentListResponseType;
import com.shanghaionstar.discover.iview.IOVDEmailView;
import com.shanghaionstar.discover.service.OVDEmailService;
import com.shanghaionstar.utils.DateFormatUtil;

import rx.Observable;
import rx.functions.Action1;
import rx.functions.Func1;

/**
 * Created by 7828wc on 16/7/21.
 */
public class OVDEmailPresenter extends BasePresenter<ManagedContentDetailDTO> {
    private IOVDEmailView view;
    private OVDEmailService service;
    private String ovdEmailTitle = "";

    public OVDEmailPresenter(IMVPScheduler mvpScheduler) {
        super(mvpScheduler);
    }

    @Override
    public void setView(IMVPView<ManagedContentDetailDTO> view) {
        super.setView(view);
        this.view = (IOVDEmailView) view;
        service = new OVDEmailService(view.onActivity());
    }

    public void getOVDEmail() {
        view.setLoading(true);
        subscription = service.getOvdList().flatMap(new Func1<GetAllContentListResponseType, Observable<ManagedContentDetailDTO>>() {
            @Override
            public Observable<ManagedContentDetailDTO> call(GetAllContentListResponseType entity) {
                if (entity != null && entity.getContentHeaderList() != null && entity.getContentHeaderList().size() > 0) {
                    ContentHeaderType headerType = entity.getContentHeaderList().get(0);
                    ovdEmailTitle = DateFormatUtil.formatOvdEmail(view.onActivity(), headerType.getTitle().replace("Vehicle Diagnostics Report:", ""));
                    return service.getOvdDetail(headerType.getId());
                } else {
                    view.onReponseSuccess(null);
                    view.setLoading(false);
                    return null;
                }
            }
        }).subscribeOn(mvpScheduler.backgroundThread())
                .observeOn(mvpScheduler.mainThread()).subscribe(new Action1<ManagedContentDetailDTO>() {
                    @Override
                    public void call(ManagedContentDetailDTO contentDetailDTO) {
                        try {
                            view.onReponseSuccess(contentDetailDTO);
                        } catch (Exception e) {
                            e.printStackTrace();
                            view.onResponseError(ExceptionUtil.toOnStarException(e));
                        }finally {
                            view.setLoading(false);
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        //抛出Error异常
                        view.onResponseError((OnStarException) throwable);
                        view.setLoading(false);
                    }
                });
    }

    public String getOvdEmailTitle() {
        return ovdEmailTitle;
    }
}
